package com.story.api.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.story.core.util.AesUtils;
import com.story.core.util.Hex;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * @author Administrator
 * @decription 重写MappingJackson2HttpMessageConverter中的writeInternal,对返回的json数据加密处理
 * @Date 2017-12-05 14:37
 */
//@Configuration
//public class RespnoseConvertConfig {
//
//    @Value("${aes.key}")
//    private String key;
//
//    @Bean
//    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
//        return new MappingJackson2HttpMessageConverter() {
//            @Override
//            protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
//                ObjectMapper mapper = new ObjectMapper();
//                String json = mapper.writeValueAsString(object);
//                //加密
//                byte[] result = null;
//                try {
//                    result = AesUtils.encrypt(json.getBytes("UTF-8"),key);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                //输出
//                outputMessage.getHeaders().set("Content-Type","text/plain;Charset=UTF-8");
//                outputMessage.getBody().write(Hex.encodeHexStr(result).getBytes());
//            }
//        };
//    }
//}
